package com.team.cluedo.app;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.team.cluedo.Game;
import com.team.cluedo.R;
import com.team.cluedo.app.utils.CluedoMenu;
import com.team.cluedo.app.waitscreens.WaitForYourTurn;

public class Move extends Activity implements OnClickListener{

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		return CluedoMenu.onCreateOptionsMenu(menu, this);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return CluedoMenu.onOptionsItemSelected(item, this);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	    setContentView(R.layout.move);
	    
	    ImageView haut = (ImageView) findViewById(R.id.ImageViewMoveHaut);
	    ImageView left = (ImageView) findViewById(R.id.ImageViewMoveGauche);
	    ImageView right = (ImageView) findViewById(R.id.ImageViewMoveDroite);
	    ImageView bas = (ImageView) findViewById(R.id.ImageViewMoveBas);
	    
	    TextView tx = (TextView ) findViewById(R.id.TextView01);
	    tx.setText("Deplacement restant : "+Game.cluedo.nbOfMove);
	    
	    
	    if(Game.cluedo.getDirections("north")){
	 	    haut.setOnClickListener(this);
	    }else{
	    	haut.setBackgroundColor(Color.BLACK);
	    }
	    if(Game.cluedo.getDirections("west")){
		    left.setOnClickListener(this);
	    }else{
	    	left.setBackgroundColor(Color.BLACK);
	    }
	    if(Game.cluedo.getDirections("east")){
	    	
		    right.setOnClickListener(this);
	    }else{
	    	right.setBackgroundColor(Color.BLACK);
	    }	   
	    if(Game.cluedo.getDirections("south")){
	    	
	 	    bas.setOnClickListener(this);
	    }else{
	    	bas.setBackgroundColor(Color.BLACK);
	    }
	   
	}

	@Override
	public void onClick(View v) {
		Intent defineIntent;
		switch (v.getId()) {
		case R.id.ImageViewMoveHaut:
			Game.cluedo.waitMove("north");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
		break;
		case R.id.ImageViewMoveBas:
			Game.cluedo.waitMove("south");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
		break;
		case R.id.ImageViewMoveGauche:
			Game.cluedo.waitMove("West");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
		break;
		case R.id.ImageViewMoveDroite:
			Game.cluedo.waitMove("east");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
		break;
		default: break;
		}
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		Intent defineIntent;
		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			Game.cluedo.waitForValidedMove();
			//if square is not a corridor
			defineIntent = new Intent(this,WaitForYourTurn.class);
			Game.cluedo.activity.startActivity(defineIntent);
	     	this.finish();
		}
		else if(keyCode == KeyEvent.KEYCODE_DPAD_UP) {
     		Game.cluedo.waitMove("east");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
			
		}
		else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			Game.cluedo.waitMove("West");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
		}
		else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			Game.cluedo.waitMove("north");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
     		
		}
		else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
     		Game.cluedo.waitMove("south");
			defineIntent = new Intent(this,Move.class);
     		this.startActivity(defineIntent);
     		this.finish();
		}
	return false;
	}
}
